﻿using System.Linq;
using System.Windows.Forms;

using JoshuaOneEight.Services;

namespace JoshuaOneEight
{
	/// <summary>
	/// Form used to edit or create a verse.
	/// </summary>
	public partial class EditForm : Form
	{
		#region Constructors

		/// <summary>
		/// Default Constructor
		/// </summary>
		public EditForm()
		{
			InitializeComponent();
		}

		#endregion Constructors

		#region EditForm Members

		/// <summary>
		/// Gets or sets the bound verse.
		/// </summary>
		public Verse Verse
		{
			get
			{
				return mVerse;
			}
			set
			{
				mVerse = value;
				_Bind();
			}
		}

		#endregion EditForm Members

		#region Private Members

		private Verse mVerse;

		/// <summary>
		/// Bind the verse to the UI.
		/// </summary>
		private void _Bind()
		{
			mTxtCategory.Text = mVerse.Category;
			mTxtTopic.Text = mVerse.Topic;
			mTxtPassage.Text = mVerse.Passage;
			mTxtText.Text = mVerse.Text;
		}

		/// <summary>
		/// Handle the Click event of the OK button.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void _HandleBtnOkClick( object sender, System.EventArgs e )
		{
			mVerse.Category = mTxtCategory.Text;
			mVerse.Topic = mTxtTopic.Text;
			mVerse.Passage = mTxtPassage.Text;
			mVerse.Text = mTxtText.Text;
		}

		#endregion Private Members

		#region Protected Members

		/// <summary>
		/// Handle the OnKeyDown event of the form.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnKeyDown( KeyEventArgs e )
		{
			if( e.KeyCode == Keys.Escape )
			{
				Close();
				e.Handled = true;
			}

			base.OnKeyDown( e );
		}

		/// <summary>
		/// Handle the OnLoad event of the form.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad( System.EventArgs e )
		{
			mTxtCategory.AvailableValues = (from verse in DataContext.Instance.Verses
											orderby Verse.Category
											select verse.Category).Distinct().ToList();

			mTxtTopic.AvailableValues = (from verse in DataContext.Instance.Verses
										 orderby Verse.Topic
										 select verse.Topic).Distinct().ToList();
		}

		#endregion Protected Members

	}
}
